package com.mzj.saas.rent;


import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.dto.HousingAllocationDTO;
import com.haixiaoke.saas.repository.pojo.HouseInspection;
import com.haixiaoke.saas.repository.pojo.HousingAllocation;
import com.haixiaoke.saas.repository.pojo.Order;
import com.haixiaoke.saas.repository.service.IHouseInspectionService;
import com.haixiaoke.saas.repository.service.IHousingAllocationService;
import com.haixiaoke.saas.repository.service.IOrderService;
import com.haixiaoke.saas.repository.vo.HousingAllocationVO;
import com.mzj.saas.annotation.CheckAuth;
import com.mzj.saas.commons.ApiData;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.commons.StatusCode;
import com.mzj.saas.mservice.community.service.AppCommunityHouseService;
import com.mzj.saas.mservice.house.vo.HouseVO;
import com.mzj.saas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 房屋配置Controller
 *
 * @author qixi
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/housing/allocation")
public class HousingAllocationController extends BaseController {
    @Autowired
    private IHousingAllocationService housingAllocationService;
    @Autowired
    LoginUserUtil loginUserUtil;
    @Autowired
    private AppCommunityHouseService appCommunityHouseService;
    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private IHouseInspectionService iHouseInspectionService;

    /**
     * 查询房屋配置列表
     */
    @GetMapping("/list")
    @CheckAuth
    public TableDataInfo list(HousingAllocation housingAllocation) {
        startPage();
        List<HousingAllocation> list = housingAllocationService.selectHousingAllocationList(housingAllocation);
        return getDataTable(list);
    }

    /**
     *
     * @param dto
     * @return
     */
    @GetMapping("/conditionList")
    public TableDataInfo conditionList(HousingAllocationDTO dto){
        startPage();
        List<HousingAllocationVO> list = housingAllocationService.conditionList(dto);
        return getDataTable(list);
    }

    /**
     * 首页条件查 查询房屋配置列表
     */
    @PostMapping("/chooseHouse")
    public TableDataInfo chooseHouse(@Valid @RequestBody HousingAllocationDTO dto) {
        List<HousingAllocationVO> list = housingAllocationService.selectHousingAllocationChooseHouse(dto);
        return getDataTable(list);
    }

    /**
     * 查询公寓房屋列表
     */
    @GetMapping("/pagesList")
    @ResponseBody
    public ResultBean<ApiData<HouseVO>> pagesList(@RequestParam(defaultValue = "0") Integer pageNumber,
                                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                                  HouseVO vo, @RequestParam(required = false) List<String> houseIdList) {
        String token = loginUserUtil.getToken();
        List<HouseVO> data = new ArrayList<>();
        ResultBean<ApiData<HouseVO>> pagesList = appCommunityHouseService.pageslist(token, pageNumber, pageSize, vo, houseIdList);
        if (ObjectUtil.isNotNull(pagesList)) {
            String code = pagesList.getCode();
            if (StatusCode.SUCCESS_CODE_10000.getErrorCode() == code) {
                data = pagesList.getResultData().getData();
                for (HouseVO houseVO : data) {
                    String houseId = houseVO.getId();
                    HousingAllocation housingAllocation = housingAllocationService.selectHousingAllocationByHouseId(houseId);
                    if (ObjectUtil.isNotNull(housingAllocation)) {
                        houseVO.setApartmentSetState(1);
                        houseVO.setHousingAllocation(housingAllocation);
                    } else {
                        houseVO.setApartmentSetState(2);
                    }
                }
            }
        }
        return pagesList;
    }

    /**
     * 获取距离 查询房屋配置列表
     */
    // @PostMapping("/list/byLocation")
    // public AjaxResult listBy( @RequestBody  HousingAllocationDTO dto) {
    //     List<HousingAllocationVO> list = housingAllocationService.selectHousingAllocationChooseHouse(dto);
    //     return success(list);
    // }

    /**
     * 查询房屋id列表
     */
    @GetMapping("/houseId/{houseId}")
    @CheckAuth
    public AjaxResult list(@PathVariable("houseId") String houseId) {
        return success(housingAllocationService.selectHousingAllocationByHouseId(houseId));
    }

    /**
     * 获取房屋配置详细信息
     */
    @GetMapping(value = "/{allocationId}")
    @CheckAuth
    public AjaxResult getInfo(@PathVariable("allocationId") Long allocationId) {
        return success(housingAllocationService.selectHousingAllocationByAllocationId(allocationId));
    }

    @PostMapping(value = "/byId/{allocationId}")
    @CheckAuth
    public AjaxResult getInfoById(@PathVariable("allocationId") Long allocationId) {
        return success(housingAllocationService.selectHousingAllocationById(allocationId));
    }

    /**
     * 新增房屋配置
     */
    @PostMapping
    @CheckAuth
    public AjaxResult add(@Valid @RequestBody HousingAllocation housingAllocation) {
        String id = loginUserUtil.getId();
        housingAllocation.setCreateBy(id);
        if (HousingAllocation.APARTMENT_TYPE_LONG.equals(housingAllocation.getApartmentType())
                && !housingAllocationService.checkLongApartmentTime(housingAllocation.getRentableStartTime(), housingAllocation.getRentableEndTime())) {
            return error("长租公寓可租时间段需要相差90天以上");
        }
        if (!housingAllocationService.checkHouseIdUnique(housingAllocation.getHouseId())) {
            return error("同一房屋不可重复新增配置");
        }
        return toAjax(housingAllocationService.insertHousingAllocation(housingAllocation));
    }

    /**
     * 批量新增房屋配置
     */
    @PostMapping("/addHousingAllocations")
    @CheckAuth
    public AjaxResult addHousingAllocations(@RequestBody HousingAllocation housingAllocation) {
        String id = loginUserUtil.getId();
        housingAllocation.setCreateBy(id);
        if (HousingAllocation.APARTMENT_TYPE_LONG.equals(housingAllocation.getApartmentType())
                && !housingAllocationService.checkLongApartmentTime(housingAllocation.getRentableStartTime(), housingAllocation.getRentableEndTime())) {
            return error("长租公寓可租时间段需要相差90天以上");
        }
        return toAjax(housingAllocationService.insertHousingAllocationList(housingAllocation));
    }

    /**
     * 修改房屋配置
     */
    @PutMapping
    @CheckAuth
    public AjaxResult edit(@Valid @RequestBody HousingAllocation housingAllocation) {
        String id = loginUserUtil.getId();
        housingAllocation.setUpdateBy(id);
        if (HousingAllocation.APARTMENT_TYPE_LONG.equals(housingAllocation.getApartmentType())
                && !housingAllocationService.checkLongApartmentTime(housingAllocation.getRentableStartTime(), housingAllocation.getRentableEndTime())) {
            return error("长租公寓可租时间段需要相差90天以上");
        }
        return toAjax(housingAllocationService.updateHousingAllocation(housingAllocation));
    }

    /**
     * 批量修改房屋上下架状态
     */
    @PutMapping("/updateApartmentState")
    @CheckAuth
    public AjaxResult updateApartmentState(@RequestBody HousingAllocation housingAllocation) {
        String id = loginUserUtil.getId();
        housingAllocation.setUpdateBy(id);
        return toAjax(housingAllocationService.updateHousingAllocationApartmentState(housingAllocation));
    }

    @CheckAuth
    @PutMapping("updateCleaningType")
    public AjaxResult updateCleaningType(@RequestBody HousingAllocation housingAllocation) {
        String id = loginUserUtil.getId();
        housingAllocation.setUpdateBy(id);
        Order order = iOrderService.selectOrderByHouseId(housingAllocation.getHouseId());
        if (ObjectUtil.isNotEmpty(order)) {
            HouseInspection houseInspection = iHouseInspectionService.selectHouseInspectionByOrderIdAndInspectionStatus(order.getOrderId(), HouseInspection.ORDER_STATE_ENTERPRISE_LEASE_HOUSE_INSPECTION);
            if (ObjectUtil.isEmpty(houseInspection)) return error("请先验房！");
        }
        return toAjax(housingAllocationService.updateHousingAllocationCleaningType(housingAllocation));
    }

    /**
     * 删除房屋配置
     */
    @DeleteMapping("/{allocationIds}")
    @CheckAuth
    public AjaxResult remove(@PathVariable Long[] allocationIds) {
        return toAjax(housingAllocationService.deleteHousingAllocationByAllocationIds(allocationIds));
    }
}
